<?php 

class Backend_Controller_Albums extends Backend_Controller_Abstract
{
	function actionDelete()
	{
		$albumId = $this->_context->post('albumId', null);
		if($albumId){
			$this->_returnAjax(
				Albums::model()->deleteByIdWithBranch($albumId)
			);
		}
	}
	
	function actionEditname()
	{
		$albumId = $this->_context->post('albumId', null);
		if($albumId){
			$name = $this->_context->post('newName', null);
			$this->_returnAjax(
				Albums::model()->update(array('name' => $name), array('id' => $albumId))
			);
		}
	}
	
	function actionAdd()
	{
		$addForm = $this->_context->post('form', null);
		if($addForm){
			$albumModel = new Albums();
			$albumModel->attributes = array_merge($albumModel->attributes, $addForm);
			if($result = $albumModel->attributesIdentity()){
				$result = $albumModel->save(true);
			}
			$this->_returnAjax(
				array(
					'result' => $result,
					'errorinfo' => $albumModel->getValidateError(),
					'addItem' => array_merge(
						$albumModel->attributes, 
						array(
							'id' => $albumModel->getConnection()->getLastInsert(), 
							'isParent' => true
						)
					)
				)
			);
		}
	}
	
	function actionEdit()
	{
		$editForm = $this->_context->post('form', null);
		if(!empty($editForm['id'])){
			$albumModel = new Albums();
			$albumModel->attributes = $editForm;
			if($result = $albumModel->attributesIdentity(array('foldername'))){
				$result = $albumModel->save(true);
			}
			$this->_returnAjax(
				array(
					'result' => $result,
					'errorinfo' => $albumModel->getValidateError(),
					'editItem' => $albumModel->attributes
				)
			);
		}else{
			$this->_returnAjax(
				array(
					'result' => false,
					'errorinfo' => array('album' => array('请选择相册'))
				)
			);
		}
	}
	
}